package com.jiandaoshou;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ExpandableListView;

import com.andview.refreshview.XRefreshView;
import com.jiandaoshou.adapter.ExPanAdapter;
import com.jiandaoshou.base.BaseActivity;
import com.jiandaoshou.entry.DingDanDetailEntry;
import com.jiandaoshou.entry.MonthOrderInfo;
import com.jiandaoshou.http.APIDefine;
import com.jiandaoshou.http.Http;
import com.jiandaoshou.utils.JsonUtil;
import com.jiandaoshou.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ZhanghuDeTailAct extends BaseActivity {
    private ExpandableListView mListView;
    private XRefreshView xRefreshView;
    private long lastRefreshTime;
    private ExPanAdapter adapter;
    private Map<String, List<DingDanDetailEntry>> childs;
    private List<MonthOrderInfo> infos = new ArrayList<>();
    private ArrayList<String> parents;
    private String shopID;
    private int page=1;
    private boolean flag=true;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
           switch (msg.what){
                   //显示前三个月的数据
               case 1:
                   infos = (List<MonthOrderInfo>) msg.obj;
                   SortData(infos);
                   for (int i = 0; i < infos.size(); i++) {
                       parents.add(infos.get(i).getMonth());
                       childs.put(infos.get(i).getMonth(), infos.get(i).getDatas());
                   }
                   adapter = new ExPanAdapter(ZhanghuDeTailAct.this, parents, childs);
                   mListView.setAdapter(adapter);
                   mListView.setGroupIndicator(null);
                   for (int i = 0; i < parents.size(); i++) {
                       mListView.expandGroup(i);
                   }
                   if(infos.size()<10){
                       xRefreshView.setLoadComplete(true);
                       xRefreshView.stopLoadMore();
                   }
                   if(flag){
                       xRefreshView.stopRefresh();
                   }else{
                       xRefreshView.stopLoadMore();
                   }
                   break;
           }
        }
    };
    @Override
    public boolean showBackLogo() {
        return true;
    }

    @Override
    protected int getContentViewRes() {
        return R.layout.activity_zhanghudetail;
    }

    @Override
    protected String getTitleText() {
        return "账户明细";
    }

    @Override
    public void init() {
        mListView = $(R.id.listview_zhanghudetial);
        xRefreshView = $(R.id.ls_refresh);
        lastRefreshTime = System.currentTimeMillis();
        // 设置是否可以下拉刷新
        xRefreshView.setPullRefreshEnable(false);
        // 设置是否可以上拉加载
        xRefreshView.setPullLoadEnable(false);
        // 设置上次刷新的时间
        xRefreshView.restoreLastRefreshTime(lastRefreshTime);
        // 设置时候可以自动刷新
        xRefreshView.setAutoRefresh(false);
        xRefreshView.setHideFooterWhenComplete(false);
        childs = new HashMap<String, List<DingDanDetailEntry>>();
        GetExpendData(page);
        mListView.setGroupIndicator(null);
        mListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView expandableListView, View view, int i, long l) {
                return true;
            }
        });

        mListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int i, int i1, long l) {
                return false;
            }
        });
        xRefreshView.setXRefreshViewListener(new XRefreshView.SimpleXRefreshListener(){
            @Override
            public void onLoadMore(boolean isSilence) {
                super.onLoadMore(isSilence);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                       xRefreshView.setLoadComplete(true);
                       xRefreshView.stopLoadMore();
                    }
                },2000);
            }

            @Override
            public void onRefresh() {
                super.onRefresh();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        xRefreshView.stopRefresh();
                    }
                },2000);
            }
        });
    }

    //获取订单数据
    private void GetExpendData(int page) {
        //请求每月前10条数据
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);//获取年份
        shopID =getIntent().getStringExtra("shopId");
        parents = new ArrayList<String>();
        HashMap<String, String> map = new HashMap<>();
        map.put("strShopID", shopID);
        map.put("strYear", year + "");
        map.put("page", page + "");
        map.put("rows", 10 + "");
        Http.post(APIDefine.TF_Interface_GetTop10EveryMonth, map, new Http.HttpCallBack() {
            @Override
            public void onHttpSuccess(String response) throws Exception {
                JSONObject object = new JSONObject(response);
                JSONArray array = object.getJSONArray("monthOrders");
                List<MonthOrderInfo> infos = new ArrayList<MonthOrderInfo>();
                JSONObject json;
                MonthOrderInfo info;
                String Month;
                DingDanDetailEntry entry;
                JSONArray array1;
                for (int i = 0; i < array.length(); i++) {
                    json = (JSONObject) array.get(i);
                    Month = json.getString("month");
                    info = new MonthOrderInfo();
                    info.setMonth(Month);
                    array1 = json.getJSONArray("orderList");
                    List<DingDanDetailEntry> entryList = new ArrayList<DingDanDetailEntry>();
                    for (int j = 0; j < array1.length(); j++) {
                        entry = JsonUtil.parser.fromJson(array1.get(j).toString(), DingDanDetailEntry.class);
                        entryList.add(entry);
                    }
                    info.setDatas(entryList);
                    infos.add(info);
                }
                Message msg = handler.obtainMessage();
                msg.obj = infos;
                msg.what = 1;
                msg.sendToTarget();
            }
        });
    }

    //按照日期排序
    private void SortData(List<MonthOrderInfo> infos) {
        Collections.sort(infos, new Comparator<MonthOrderInfo>() {
            @Override
            public int compare(MonthOrderInfo t, MonthOrderInfo t1) {
                String year = t.getMonth().substring(0, 4);
                String year1 = t.getMonth().substring(0, 4);
                String month = t.getMonth().substring(5, 7);
                String month1 = t.getMonth().substring(5, 7);
                LogUtil.e(year + "  " + month);
                if (Integer.valueOf(year) != Integer.valueOf(year1)) {
                    return year.compareTo(year1);
                } else {
                    return month.compareTo(month1);
                }
            }
        });
    }
}
